package com.iss.card.view.mycard.detail;

import java.util.ArrayList;

import android.R.bool;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.iss.card.R;
import com.iss.card.R.array;
import com.iss.card.common.ApplicationManager;
import com.iss.card.common.FunctionUtil;
import com.iss.card.common.IBackKeyListener;
import com.iss.card.common.Tools;
import com.iss.card.model.UserInfo;
import com.iss.card.view.model.CardDetailHistoryModel;
import com.iss.card.view.model.CardDetailReturnOrderModel;
import com.iss.card.view.model.ResponseBean;
import com.iss.card.view.mycard.CardBagModel;
import com.iss.card.view.mycard.CardBagUtils;
import com.iss.card.view.mycard.MyCardDetailActivity;
import com.iss.card.webservice.BaseModel;
import com.iss.card.webservice.WebServiceAsyncService;
import com.iss.card.webservice.WebServiceAsyncService.Operation;
import com.iss.card.webservice.WebServiceAsyncService.PostExecute;
import com.iss.card.webservice.WebServiceAsyncServiceHelper;
import com.iss.card.webservice.WebServiceManager;
import com.iss.card.webservice.WebServiceManager.WebServiceRegistrationParameter;
import com.iss.card.webservice.WebServiceManager.WebServiceUrlPortManager;
import com.iss.card.webservice.WebServiceUtils;

public class CardDetailHistroyFragment extends Fragment implements IBackKeyListener{

	

	private CardBagModel mCardBagModel;
	private FragmentActivity mContext;
	private View mainView;
	private ListView itemContainer;
	private UserInfo usrinInfo;
	private CardBagModel  cardBagModel;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		cardBagModel = (CardBagModel) getArguments().getSerializable(ApplicationManager.KEY_MYCARD_BRIEF_CARD);
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mContext = (FragmentActivity)activity;
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mainView = new View(mContext).inflate(mContext, R.layout.card_history, null);
		itemContainer = (ListView) mainView.findViewById(R.id.history_item_container);
		informationCollection();
		return mainView;
		
	}
	
	private void informationCollection(){
		usrinInfo = FunctionUtil.getCurrentLogInUser();
		String[] parameterValues = {cardBagModel.getCardId(), 
				usrinInfo.getUsrMobile()};
		WebServiceAsyncService asyncService = new WebServiceAsyncService();
		asyncService.setParameterToAsyncService(
				mContext, 
				asyncService.getNextToken(), 
				Operation.OP_SERVICE_ARG_CARD_HISTORY_TRACE, 
				WebServiceRegistrationParameter.PRODUCT_CARD_HISTORY_TRACE_PARAMETER, 
				parameterValues, 
				WebServiceUrlPortManager.ACTION_URL_PORT_CARD_HISTORY_TRACE,
				this,
				WebServiceAsyncServiceHelper.TYPE_HTTP_GET_INSTREAM
				
				);
		asyncService.setPostExecute(new PostExecute() {
			@Override
			public void onAsyncServiceComplete(int token, Object envelope,int index) {
				ResponseBean response = WebServiceUtils.getObjectResultToBaseInfo(envelope, CardDetailHistoryModel.class);
				ArrayList<CardDetailHistoryModel> historyTraceInfos = 
						(ArrayList<CardDetailHistoryModel>) response.getReturnList();
			
				itemContainer.setAdapter(new HistoryListAdapter(historyTraceInfos, mContext));
			}
		});
	}

	@Override
	public void cancelUpdate(Object... params) {
		// TODO Auto-generated method stub
		
	}

	class HistoryListAdapter extends BaseAdapter{

		private ArrayList<CardDetailHistoryModel> mItemList;
		private Context mContext;
		private LayoutInflater inflater;
		public HistoryListAdapter(ArrayList<CardDetailHistoryModel> list, FragmentActivity context){
			this.mItemList = list;
			this.mContext = context;
			inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mItemList.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			HistoryItemViewHolder holder = new HistoryItemViewHolder();
			CardDetailHistoryModel model = mItemList.get(position);
			final LinearLayout itemContainer;
			final String _id = model.getOrderID();
			
			if(convertView == null){
				convertView = inflater.inflate(R.layout.card_history_item, null);
				
				holder.historyCustomeAmount = (TextView) convertView.findViewById(R.id.history_customed_amount);
				holder.historyCustomedStore = (TextView) convertView.findViewById(R.id.history_customed_store);
				holder.historyCustomedValue = (TextView) convertView.findViewById(R.id.history_customed_value);
				holder.historyDiscount = (TextView) convertView.findViewById(R.id.history_discount);
				holder.historyOrderDate = (TextView) convertView.findViewById(R.id.history_order_date);
				holder.historyOrderId = (TextView) convertView.findViewById(R.id.history_order_id);
				holder.historyOrderPayer = (TextView) convertView.findViewById(R.id.history_order_payer);
				holder.historySaved = (TextView) convertView.findViewById(R.id.history_saved);
				holder.historyUseAmount = (TextView) convertView.findViewById(R.id.history_use_amount);
				holder.historyUseCash = (TextView) convertView.findViewById(R.id.history_use_cash);
				holder.historyUsePoints = (TextView) convertView.findViewById(R.id.history_use_points);
				
				holder.returnOrderButton = (Button)convertView.findViewById(R.id.return_order_btn);
				itemContainer = holder.returnItemContainer = (LinearLayout)convertView.findViewById(R.id.return_order_container);
				
				convertView.setTag(holder);
			}else{
				holder = (HistoryItemViewHolder)convertView.getTag();
				itemContainer = holder.returnItemContainer;
			}
			holder.returnItemContainer.setVisibility(View.GONE);
			
			if(/*model.getOrderStatus().equals(ApplicationManager.HISTORY_ORDER_STATUS_PART_RETURN )
					|| */model.getOrderStatus().equals(ApplicationManager.HISTORY_ORDER_STATUS_ALL_RETURN )){
				holder.returnOrderButton.setVisibility(View.VISIBLE);
			} else {
				holder.returnOrderButton.setVisibility(View.GONE);
			}
			
			holder.returnOrderButton.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if(!Tools.isNetworkConnected(mContext)){
						Tools.showToast(mContext, R.string.network_error);
						return;
					}
					if(View.VISIBLE == itemContainer.getVisibility()){
						itemContainer.setVisibility(View.GONE);
					}else{
//						itemContainer.setVisibility(View.VISIBLE);
						getReturnItem(_id,itemContainer);
					}
					
				}
			});
			
			holder.historyCustomeAmount.setText(mContext.getString(R.string.card_detail_customed_amount)
					+ model.getConsumedNumber());
			holder.historyCustomedStore.setText(mContext.getString(R.string.card_detail_customed_store)
					+model.getConsumedStore());
			holder.historyCustomedValue.setText(mContext.getString(R.string.card_detail_order_value)
					+model.getOrderAmount());
			holder.historyDiscount.setText(mContext.getString(R.string.card_detail_use_discont)+
					model.getDisplayedDiscount());
			holder.historyOrderDate.setText(mContext.getString(R.string.card_detail_order_date)+ 
					model.getOrderDate());
			holder.historyOrderId.setText(mContext.getString(R.string.card_detail_order_id) + 
					model.getOrderNo());
			holder.historyOrderPayer.setText(mContext.getString(R.string.card_detail_order_payer) + 
					model.getOrderPayer());
			holder.historySaved.setText(mContext.getString(R.string.card_detail_use_saved) + 
					model.getUseStoredValue());
			holder.historyUseAmount.setText(mContext.getString(R.string.card_detail_use_count) + 
					model.getUseNumber());
			holder.historyUseCash.setText(mContext.getString(R.string.card_detail_use_cash) +
					model.getUseCash());
			holder.historyUsePoints.setText(mContext.getString(R.string.card_detail_use_points) + 
					model.getUsePoints());
			
			return convertView;
		}
	}

	
	static class HistoryItemViewHolder{
		TextView historyOrderId, historyOrderDate, historyCustomedStore,
		historyCustomeAmount, historyCustomedValue, historyUseAmount,
		historySaved, historyOrderPayer, historyUsePoints, historyDiscount,
		historyUseCash;
		Button returnOrderButton;
		LinearLayout returnItemContainer;
		
	}
	
//	for add more return order items
//  unused now
	static class ReturnOrderitemViewHolder{
		TextView returnOrderId, returnOrderDate, returnCustomedStore,
		returnCustomedAmount, returnOrderValue, returnSavedValue, 
		returnPoints,returnCash;
	}
//	end
	
	public void getReturnItem(String id, final View view){
		
		String[] parameterValues = {id};
		WebServiceAsyncService asyncService = new WebServiceAsyncService();
		asyncService.setParameterToAsyncService(
				mContext, 
				asyncService.getNextToken(), 
				Operation.OP_SERVICE_ARG_CARD_HISTORY_TRACE, 
				WebServiceRegistrationParameter.PARAMETERS_RETURN_ORDER, 
				parameterValues, 
				WebServiceUrlPortManager.ACTION_URL_PORT_RETURN_ORDER,
				this,
				WebServiceAsyncServiceHelper.TYPE_HTTP_GET_INSTREAM
				
				);
		asyncService.setPostExecute(new PostExecute() {
			@Override
			public void onAsyncServiceComplete(int token, Object envelope,int index) {
				ResponseBean response = WebServiceUtils.getObjectResultToBaseInfo(envelope, CardDetailReturnOrderModel.class);
				ArrayList<CardDetailReturnOrderModel> ReturnOrderItmes = 
						(ArrayList<CardDetailReturnOrderModel>) response.getReturnList();
				if(ReturnOrderItmes != null){
					CardDetailReturnOrderModel model = null;
					for (CardDetailReturnOrderModel info : ReturnOrderItmes) {
						model = (CardDetailReturnOrderModel) info;
						break;
					}
					TextView returnOrderId = (TextView) view.findViewById(R.id.return_order_id);
					TextView returnOrderDate = (TextView) view.findViewById(R.id.return_order_date);
					TextView returnCustomedStore = (TextView) view.findViewById(R.id.return_customed_store);
					TextView returnCustomedAmount = (TextView) view.findViewById(R.id.return_customed_amount);
					TextView returnOrderValue = (TextView) view.findViewById(R.id.return_order_value);
					TextView returnSavedValue = (TextView) view.findViewById(R.id.return_saved_value);
					TextView returnPoints = (TextView) view.findViewById(R.id.return_points);
					TextView returnCash = (TextView) view.findViewById(R.id.return_cash);
					
					returnOrderId.setText(getString(R.string.return_order_order_id) + 
							model.getOrderNo());
					returnOrderDate.setText(getString(R.string.return_order_order_date) + 
							model.getOrderDate());
					returnCustomedStore.setText(getString(R.string.return_order_customed_store) + 
							model.getReturnStoreName());
					returnOrderValue.setText(getString(R.string.return_order_value) + 
							model.getReturnAmount());
					returnSavedValue.setText(getString(R.string.return_order_saved) + 
							model.getReturnValue());
					returnCash.setText(getString(R.string.return_order_cash) + 
							model.getReturnCash());
					returnPoints.setText(getString(R.string.return_order_points) + 
							model.getReturnPoints());
					returnCustomedAmount.setText(getString(R.string.return_order_customed_amount) + 
							model.getReturnNumber());
					
					view.setVisibility(View.VISIBLE);
				}else 
					return;
				
			}
		});
	}
}
